package net.mgsx.product.t2m.modisco.utility;
import net.mgsx.product.t2m.modisco.tools.QualityUtils;

import org.eclipse.gmt.modisco.java.AbstractMethodDeclaration;
import org.eclipse.gmt.modisco.java.BodyDeclaration;
import org.eclipse.gmt.modisco.java.ClassDeclaration;
import org.eclipse.gmt.modisco.infra.query.core.exception.ModelQueryExecutionException;
import org.eclipse.gmt.modisco.infra.query.core.java.IJavaModelQuery;
import org.eclipse.gmt.modisco.infra.query.core.java.ParameterValueList;

/**
 * Java model query to identify utility class.
 * Utility class only contains static methods or static attributes
 * 
 * @author mgsx
 *
 */
public class IsUtilityClass implements IJavaModelQuery<ClassDeclaration, Boolean> {
	
	public Boolean evaluate(final ClassDeclaration context, final ParameterValueList parameterValues)
			throws ModelQueryExecutionException {
		Boolean result = null;
		if(context != null)
		{
			if(QualityUtils.isNotProxy(context))
			{
				int nbStatic = 0;
				int nbNonStatic = 0;
				for(BodyDeclaration declaration : context.getBodyDeclarations())
				{
					if(declaration instanceof AbstractMethodDeclaration)
					{
						if(declaration.getModifier() != null &&
								declaration.getModifier().isStatic())
						{
							nbStatic++;
						}
						else
						{
							nbNonStatic++;
						}
					}
				}
				result = nbStatic > 0 && nbNonStatic == 0;
			}
			else
			{
				result = false;
			}
		}
		return result;
	}
}
